// personal.js
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

        var that = this

        //调用应用实例的方法获取全局数据
        app.getUserInfo(function(userInfo) {
            //更新数据
            that.setData({
                userInfo: userInfo
            })
            console.log(userInfo)
            that.setData({
                name: userInfo.nickName,
                headImgUrl: userInfo.avatarUrl
            })

            wx.request({
                url: app.data.baseUrl + '/AibraUsb/userInfo/personHub.do',
                data: {
                    openId: app.globalData.openid
                },
                success: function(res) {

                    that.setData({
                        headImgUrl: res.data.headImgUrl,
                        points: res.data.points,
                        balance: res.data.balance,
                        mobile: res.data.mobile,
                        phoneNum: options.phoneNum,
                        totalPoints: res.data.totalPoints
                    })
                }
            })
            wx.request({
                url: app.data.baseUrl + '/AibraUsb/userInfo/add.do',
                data: {
                    name: that.data.name,
                    headImgUrl: that.data.headImgUrl,
                    openId: app.globalData.openid,
                    userId: 0
                },
                success: function(res) {
                    console.log("成功")

                },
                fail: function() {
                    console.log("失败")
                }
            })
        })


    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        var that = this
        wx.request({
            url: app.data.baseUrl + '/AibraUsb/userInfo/personHub.do',
            data: {
                openId: app.globalData.openid
            },
            success: function(res) {
                console.log('reswww', res)
                that.setData({
                    headImgUrl: res.data.headImgUrl,
                    points: res.data.points,
                    balance: res.data.balance,
                    mobile: res.data.mobile,
                    totalPoints: res.data.totalPoints
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    bindshoppingCart: function() {
        wx.navigateTo({
            url: 'shoppingCart/shoppingCart',
        })
    },
    bindpersonData: function() {
        var that = this;
        wx.navigateTo({
            url: 'personalData/personalData?mobile=' + that.data.mobile,
        })
    },
    bindPhonenum: function() {
        wx.navigateTo({
            url: 'bindPhoneNum/bindPhoneNum',
        })
    },
    bindmyPartner: function() {
        wx.navigateTo({
            url: 'myPartner/myPartner',
        })
    },
    bindmyBalance: function() {
        var that = this;
        wx.navigateTo({
            url: "myBalance/myBalance?balance=" + that.data.balance,
        })
    },
    binddeliveryAddress: function() {
        wx.navigateTo({
            url: "deliveryAddress/deliveryAddress"
        })
    },
    bindcoupon: function() {
        wx.navigateTo({
            url: "coupon/coupon"
        })
    },
    bindorder: function() {
        wx.navigateTo({
            url: "order/order"
        })
    },
    bindmyErweima: function() {
        wx.navigateTo({
            url: 'myErweima/myErweima'
        })
    }

})